package com.firefly.admin.config.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.enums.ResultCodeEnum;
import com.firefly.admin.common.utils.RedisUtil;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.config.entity.FySystemConfiguration;
import com.firefly.admin.config.mapper.FySystemConfigurationMapper;
import com.firefly.admin.config.service.FySystemConfigurationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.firefly.admin.config.vo.SystemConfigurationQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class FySystemConfigurationServiceImpl implements FySystemConfigurationService {

    @Autowired
    private FySystemConfigurationMapper fySystemConfigurationMapper;

    /**
     * 查询所有配置信息
     *
     * @return 配置信息列表
     */
    @Override
    public List<FySystemConfiguration> getAllConfig() {
        return fySystemConfigurationMapper.selectList(null);
    }

    /**
     * 分页查询系统配置接口
     *
     * @param systemConfigurationQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FySystemConfiguration>> getSystemConfigurationList(SystemConfigurationQueryVo systemConfigurationQueryVo) {
        log.info("###########FySystemConfigurationServiceImpl getSystemConfigurationList param = {}", JSONObject.toJSONString(systemConfigurationQueryVo));
        LambdaQueryWrapper<FySystemConfiguration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(systemConfigurationQueryVo.getConfigKey()), FySystemConfiguration::getConfigKey, systemConfigurationQueryVo.getConfigKey());
        queryWrapper.like(StringUtils.isNotBlank(systemConfigurationQueryVo.getConfigDesc()), FySystemConfiguration::getConfigDesc, systemConfigurationQueryVo.getConfigDesc());
        queryWrapper.eq(FySystemConfiguration::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        queryWrapper.orderByDesc(FySystemConfiguration::getConfigId);
        Page<FySystemConfiguration> page = new Page<>(systemConfigurationQueryVo.getPage(), systemConfigurationQueryVo.getLimit());
        Page<FySystemConfiguration> systemConfigurationPage = fySystemConfigurationMapper.selectPage(page, queryWrapper);
        return Result.success(systemConfigurationPage);
    }

    /**
     * 获取系统配置信息接口
     *
     * @param configId 配置ID
     * @return 配置信息
     */
    @Override
    public Result<FySystemConfiguration> getSystemConfiguration(Long configId) {
        if (configId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FySystemConfiguration fySystemConfiguration = fySystemConfigurationMapper.selectById(configId);
        if (fySystemConfiguration == null) {
            return Result.getResult(ResultCodeEnum.SYSTEM_DICTIONARY_NOT_FOUND);
        }
        return Result.success(fySystemConfiguration);
    }

    /**
     * 系统配置信息编辑接口
     *
     * @param fySystemConfiguration 配置信息
     * @return 操作结果
     */
    @Override
    public Result<FySystemConfiguration> update(FySystemConfiguration fySystemConfiguration) {
        if (fySystemConfiguration == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long configId = fySystemConfiguration.getConfigId();
        if (configId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FySystemConfiguration systemDictionary = fySystemConfigurationMapper.selectById(configId);
        if (systemDictionary == null) {
            return Result.getResult(ResultCodeEnum.SYSTEM_CONFIGURATION_NOT_FOUND);
        }
        String configKey = fySystemConfiguration.getConfigKey();
        if (StringUtils.isBlank(configKey)) {
            return Result.getResult(ResultCodeEnum.SYSTEM_CONFIGURATION_KEY_NOT_NULL);
        }
        if (!configKey.equals(fySystemConfiguration.getConfigKey())) {
            return Result.getResult(ResultCodeEnum.SYSTEM_CONFIGURATION_KEY_NOT_ALLOW_UPDATE);
        }
        int rows = fySystemConfigurationMapper.updateById(fySystemConfiguration);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        //刷新redis
        RedisUtil.hset(Constants.Redis.CONFIG_KEY, fySystemConfiguration.getConfigKey(), fySystemConfiguration.getConfigValue());
        return Result.success(fySystemConfiguration);
    }

    /**
     * 通过配置项KEY获取系统配置值接口
     *
     * @param configKey 配置KEY
     * @return 系统配置值
     */
    @Override
    public Result<String> getConfigurationValue(String configKey) {
        if (StringUtils.isBlank(configKey)) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        String configValue = "";
        LambdaQueryWrapper<FySystemConfiguration> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemConfiguration::getConfigKey, configKey);
        List<FySystemConfiguration> configurationList = fySystemConfigurationMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(configurationList)) {
            return Result.getResult(ResultCodeEnum.SYSTEM_CONFIGURATION_NOT_FOUND);
        }
        configValue = configurationList.get(0).getConfigValue();
        return Result.success("操作成功", configValue);
    }
}
